layui.use(['table','form','jquery','treetable', 'backend'],function () {
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    let backend = layui.backend;
    let treetable = layui.treetable;

    var render = function () {
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            skin:'line',
            method:'post',
            treeDefaultClose: true,
            toolbar:'#toolbar',
            elem: '#table',
            url: '/admin/menu/index',
            page: false,
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'id', title: '权限id'},
                    {field: 'title', minWidth: 200, title: '权限名称'},
                    {field: 'rule', minWidth: 200, title: '权限规则'},
                    {field: 'icon', title: '图标',templet:'#icon'},
                    {field: 'type', title: '权限类型',templet:'#type'},
                    {field: 'status', title: '状态',templet:'#status'},
                    {field: 'target', title: '打开方式',templet:'#targget'},
                    {field: 'weigh', title: '权重'},
                    {title: '操作',templet: '#bar', width: 150, align: 'center'}
                ]
            ]
        })
    };

    render();

    table.on('tool(table)',function(obj){
        if (obj.event === 'remove') {
            remove(obj);
        } else if (obj.event === 'edit') {
            edit(obj);
        } else if (obj.event === 'add') {
            add(obj.data.id)
        }
    });


    table.on('toolbar(table)', function(obj){
        if(obj.event === 'add'){
            add();
        } else if(obj.event === 'refresh'){
            refresh();
        } else if(obj.event === 'batchRemove'){
            batchRemove(obj);
        }  else if(obj.event === 'expandAll'){
            treetable.expandAll("#table");
        } else if(obj.event === 'foldAll'){
            treetable.foldAll("#table");
        } else if (obj.event === 'createjson') {
            event_createjson(obj);
        }
    });

    function add (pid = 0){
        backend.api.open('/admin/menu/show?pid=' + pid, '新增', {
            callback : function () {
                refresh();
            }
        });
    }

    function edit (obj){
        backend.api.open('/admin/menu/show?id=' + obj.data.id, '修改', {
            callback : function () {
                refresh();
            }
        });
    }

    function event_createjson(obj) {
        layer.confirm('确定生成初始化菜单json吗?', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: "/admin/menu/createjson",
                dataType:'json',
                type:'post',
                success:function(result){
                    layer.close(loading);
                    if(result.code == 1){
                        layer.msg(result.msg,{icon:1,time:1000});
                    }else{
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                },
                complete : function () {
                    layer.close(loading);
                }
            })
        });
    }

    function remove (obj){
        layer.confirm('如果含有子项，将一同删除，确定要删除吗？', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: "/admin/menu/del",
                dataType:'json',
                data : {ids : [obj.data['id']]},
                type:'post',
                success:function(result){
                    layer.close(loading);
                    if(result.code == 1){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            refresh();
                        });
                    }else{
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                },
                complete : function () {
                    layer.close(loading);
                }
            })
        });
    }

    function refresh() {
        render();
    }

    function batchRemove () {
        var data = table.checkStatus('table').data;
        var ids = [];
        $.each(data, function (k, v) {
            ids.push(v.id);
        });
        if (ids.length <= 0) {
            layer.msg("请选择要删除的数据",{icon:2,time:1000});
        }
        layer.confirm('确定删除' + ids.length + '条数据吗?', {icon: 3, title:'提示'}, function(index){
            let loading = layer.load();
            $.ajax({
                url: "/admin/menu/del",
                dataType:'json',
                type:'post',
                data : {ids:ids},
                success:function(result){
                    layer.close(loading);
                    if(result.code == 1){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            refresh();
                        });
                    }else{
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                },
                complete : function () {
                    layer.close(loading);
                    layer.close(index);
                }
            })
        });
    }
});
